package org.example.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class SelectSort {

    public static void main(String[] args) {
       /* int[] arr = {3, 2, 7, 5, 4, 10, 6, 1};
        selectSort(arr);
        System.out.println(Arrays.toString(arr));*/
        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++){
            arr[i] = (int) (Math.random()*8000000);
        }
        System.out.println("当前时间：" + System.currentTimeMillis());
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
        selectSort(arr);
        System.out.println("计算后的时间：" + System.currentTimeMillis());
        System.out.println();
    }

    public static void selectSort(int[] arr){

        for (int i=0; i<arr.length - 1; i++){
            int minIndex = i;
            int min = arr[i];
            //遍历，找出最小下标
            for (int j=i+1; j<arr.length ; j++){
                if (min > arr[j]){
                    minIndex = j;
                    min = arr[j];
                }
            }
            //如果最小下标不是i，则两者替换
            if (minIndex != i){
                arr[minIndex] = arr[i];
                arr[i] = min;
            }

        }
    }

}
